package com.zxl.working.controller;

import com.zxl.working.entity.admin.SysUser;
import com.zxl.working.entity.backend.InvocerAlert;
import com.zxl.working.entity.backend.SellerBill;
import com.zxl.working.entity.backend.SellerTax;
import com.zxl.working.entity.backend.ServiceInfo;
import com.zxl.working.service.ServiceService;
import com.zxl.working.utils.CommUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
@CrossOrigin
@RestController
@RequestMapping("/service")
public class ServiceController {

    private Logger logger = LogManager.getLogger(ServiceController.class);
    @Autowired
    private ServiceService serviceService;

    @Resource
    private CommUtil commUtil;

    @Value("${local.filePath}")
    private String filePath;

    @Value("${local.fileUrl}")
    private String fileUrl;

    @PostMapping("/qryServiceInvoice")
    public String qryServiceInvoice(HttpServletRequest request,@RequestBody String params){
        ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");
        if(ObjectUtils.isEmpty(serviceInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return serviceService.qryServiceInvoice(serviceInfo.getServiceId(),params);
    }
    
    @RequestMapping("/export")
    public String export(HttpServletResponse resp,HttpServletRequest request,Integer status){
        ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");
        if(ObjectUtils.isEmpty(serviceInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return serviceService.export(resp,serviceInfo.getServiceId(),status);
    }

    /*
    重写接口，去掉文件上传部分
    @PostMapping("/updServiceInvoice")
    public String updServiceInvoice(HttpServletRequest request, @RequestParam("file") MultipartFile file, SellerBill sellerBill){
        try {
            ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");
            if (ObjectUtils.isEmpty(serviceInfo)) {
                return commUtil.returnStr(-99, "请重新登录", "");
            }

            String filename = System.currentTimeMillis() + file.getOriginalFilename();
            String path = filePath + "/" + filename;
            FileUtils.writeByteArrayToFile(new File(path), file.getBytes());

            sellerBill.setPicUrl(fileUrl+filename);

            return serviceService.updServiceInvoice(sellerBill);
        }catch (Exception e){
            return commUtil.returnFalse(e);
        }
    }
    */

    @PostMapping("/updServiceInvoice")
    public String updServiceInvoice(HttpServletRequest request, SellerBill sellerBill){
        try {
            ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");

            if (ObjectUtils.isEmpty(serviceInfo)) {
                return commUtil.returnStr(-99, "请重新登录", "");
            }


            return serviceService.updServiceInvoice(serviceInfo.getServiceId(),sellerBill);
        }catch (Exception e){
            return commUtil.returnFalse(e);
        }
    }
    
    @PostMapping("/updexpress")
    public String updexpress(HttpServletRequest request, SellerBill sellerBill){
        try {
            ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");

            if (ObjectUtils.isEmpty(serviceInfo)) {
                return commUtil.returnStr(-99, "请重新登录", "");
            }


            return serviceService.updexpress(sellerBill);
        }catch (Exception e){
            return commUtil.returnFalse(e);
        }
    }
    
    
    /**
     * 增加发票预警
     * @param request
     * @param invoiceMoney
     * @param invoiceMail
     * @return
     */
    @PostMapping("/updateInvoice")
    public String updateInvoice(HttpServletRequest request,Integer invoiceMoney,String invoiceMail,Integer invoiceSum){
        try {
            ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");

            if (ObjectUtils.isEmpty(serviceInfo)) {
                return commUtil.returnStr(-99, "请重新登录", "");
            }


            return serviceService.updateInvoice(serviceInfo.getServiceId(),invoiceMoney,invoiceMail,invoiceSum);
        }catch (Exception e){
            return commUtil.returnFalse(e);
        }
    }
    
    @PostMapping("/findByInvocer")
    public String findByInvocer(HttpServletRequest request){
    	try {
            ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");

            if (ObjectUtils.isEmpty(serviceInfo)) {
                return commUtil.returnStr(-99, "请重新登录", "");
            }
            return serviceService.findByInvocer(serviceInfo.getServiceId());
        }catch (Exception e){
            return commUtil.returnFalse(e);
        }
    }
    
    
    
    
    

    @PostMapping("/qryTaxByServiceId")
    public String qryTaxByServiceId(HttpServletRequest request,@RequestBody String params){
        ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");
        if(ObjectUtils.isEmpty(serviceInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return serviceService.qryTaxByServiceId(serviceInfo.getServiceId(),params);
    }

    @PostMapping("/insTax")
    public String insTax(HttpServletRequest request, @RequestParam("file") MultipartFile file, SellerTax sellerTax){

        try {
            SysUser sysUser = (SysUser) request.getSession().getAttribute("loginUser");
            ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");
            if (ObjectUtils.isEmpty(serviceInfo)) {
                return commUtil.returnStr(-99, "请重新登录", "");
            }

            String filename = System.currentTimeMillis() + file.getOriginalFilename();
            String path = filePath + "/" + filename;
            FileUtils.writeByteArrayToFile(new File(path), file.getBytes());

            sellerTax.setTaxPic(fileUrl+filename);

            return serviceService.insTax(sysUser,serviceInfo.getServiceId(),sellerTax);
        }catch (Exception e){
            return commUtil.returnFalse(e);
        }
    }

    @PostMapping("/billFail")
    public String billFail(HttpServletRequest request,@RequestBody String params){
        ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");
        if(ObjectUtils.isEmpty(serviceInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return serviceService.billFail(params);
    }

    @PostMapping("/qrySellerInfoByService")
    public String qrySellerInfoByService(HttpServletRequest request){
        ServiceInfo serviceInfo = (ServiceInfo) request.getSession().getAttribute("serviceInfo");
        if(ObjectUtils.isEmpty(serviceInfo)){
            return commUtil.returnStr(-99,"请重新登录","");
        }

        return serviceService.qrySellerInfoByService(serviceInfo.getServiceId());
    }

}
